package com.cooba.adapter;


import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.cooba.R;
import com.cooba.bean.MyLivingBean;
import com.cooba.utils.CommonUtil;
import com.cooba.utils.Utils;
import com.cooba.views.GlideRoundTransform;
import com.lidroid.xutils.BitmapUtils;

import java.util.List;

public class LivingListAdapter extends BaseAdapter {
    private final BitmapUtils bitmapUtils;
    private final int cell_height;
    private final int cell_width;
    private Context context;
    private List<MyLivingBean.LiveListBean> list;

    public List<MyLivingBean.LiveListBean> getList() {
        return list;
    }

    public void setList(List<MyLivingBean.LiveListBean> list) {
        this.list = list;
        Log.d("wei", "" + list.size());
        this.notifyDataSetChanged();
    }

    public void addNewData(List<MyLivingBean.LiveListBean> datas) {
        if (datas != null && list != null) {
            this.list.addAll(0, datas);
            this.notifyDataSetChanged();
        }
    }

    public void addMoreData(List<MyLivingBean.LiveListBean> datas) {
        Log.d("wei", "" + datas.size() + "," + list.size());
        if (datas != null && list != null) {
            list.addAll(list.size(), datas);
            this.notifyDataSetChanged();
        }
    }

    public LivingListAdapter(Context context, List<MyLivingBean.LiveListBean> list) {
        this.context = context;
        this.list = list;
        bitmapUtils = new BitmapUtils(context);
        /**
         * 设置默认的图片展现、加载失败的图片展现
         */
        bitmapUtils.configDefaultLoadingImage(R.mipmap.default_icon1);
        bitmapUtils.configDefaultLoadFailedImage(R.mipmap.default_icon1);
        int width = context.getResources().getDisplayMetrics().widthPixels;
        //int height = (int) (width * 0.56);
        cell_width= width-CommonUtil.dip2px(context,20);
        cell_height=(int)(cell_width*0.56);
    }

    @Override
    public int getCount() {
        if (list.size() == 0) {
            return 0;
        }
        return list.size();
    }

    @Override
    public MyLivingBean.LiveListBean getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder holder = null;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.living_list_item, parent, false);
            holder.title = (TextView) convertView.findViewById(R.id.living_list_item_title);
            holder.name = (ImageView) convertView.findViewById(R.id.living_list_item_living_video);
            holder.num = (TextView) convertView.findViewById(R.id.living_list_item_num);
            holder.pic = (ImageView) convertView.findViewById(R.id.living_list_item_iv);
            convertView.setTag(holder);

        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.title.setText(list.get(position).getName());
        //holder.name.setText(list.get(position).getUser_name());
        holder.num.setText(list.get(position).getPraiseNum()+"人参与");
        holder.pic.setLayoutParams(new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, cell_height));
        bitmapUtils.display(holder.pic,list.get(position).getPic());
        //Glide.with(context).load(list.get(position).getPic()).placeholder(R.mipmap.default_icon1).transform(new GlideRoundTransform(context,6)).into(holder.pic);

        return convertView;
    }


    class ViewHolder {
        TextView title;
        TextView num;
        ImageView pic;
        ImageView name;

    }

}
